var prefix = ctx + "md/technical"
$().ready(function() {
	validateRule();
	deptSelectLoad();
	$("#allowComment").on('click', function(){
		if ($("#allowComment").is(':checked')) {
			console.log("在全量的状态下");
			$("#changeType").val("1");
			
		} else {
			console.log("在增量的状态下");
			$("#changeType").val("2");
		}
	});
	
	var textField = document.getElementById("targetTableName"), //获取表单域
    startText = textField.value; //获取开头字符串
	textField.onkeyup = function () {
	    (textField.value.indexOf("T_") === 0) || (textField.value = "T_");
	};

});

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	$.ajax({
		cache : true,
		type : "POST",
		url :prefix + "/doEdit",
		data : $('#signupForm').serialize(),
		async : false,
		error : function(request) {
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("保存成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.alert(data.msg)
			}
		}
	});
}
function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			}
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			}
		}
	})
}

function deptSelectLoad() {
	var oldDeptId = $("#oldDeptId").val();
	var html = "";
	$.ajax({
		url : '/sys/dept/list',
		data : {
			limit : 10,
			offset : 0,
			sourceName : ""
		},
		success : function(data) {
			// 加载数据
			var rows = data;
			for (var i = 0; i < rows.length; i++) {
				if(oldDeptId == rows[i].deptId){
					html += '<option selected="selected" value="' + rows[i].deptId + '">' + rows[i].name + '</option>';
				}else{
					html += '<option value="' + rows[i].deptId + '">' + rows[i].name + '</option>';
				}
			}
			$(".dept-select").html(html);
			$(".dept-select").chosen({
				search_contains: true,
				maxHeight : 200
			});
			// 点击事件
			$('.dept-select').on('change', function(e, params) {
				$("#deptId").val(params.selected);
			});
		}
	});
}